package research.toe.duangjarern;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import research.toe.duangjarern.Helper.NetWorkHelper;

/**
 * Created by nipitponmuensan on 2/26/15 AD.
 */
public class LoginActivity extends Activity {

    private String strUse = "";
    private String strPss = "";
    private TextView UserName;
    private TextView Password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        new startAppTask().execute();

        UserName = (TextView) findViewById(R.id.txtusername);
        Password = (TextView) findViewById(R.id.txtpassword);


        Button btnLogin = (Button) findViewById(R.id.btnlogin);
        btnLogin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                strUse = UserName.getText().toString();
                strPss = Password.getText().toString();
                if (strUse.equals("admin") && strPss.equals("admin")) {
                    Intent intent = new Intent(getApplicationContext(), MainMenu.class);
                    startActivity(intent);

                }
                //new logInTask().execute(strUse,strPss);
            }
        });

    }

    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("ออกจาก Application")
                .setMessage("คุณต้องการออกจาก Application หรือไม่?")
                .setPositiveButton("ใช่", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }

                })
                .setNegativeButton("ไม่", null)
                .show();
    }


    //Check Internet
    private class startAppTask extends AsyncTask<String, Void, Object> {

        private ProgressDialog pdia;
        private boolean ChcekerResult = false;

        protected Object doInBackground(String... params) {
            // TODO Auto-generated method stub
            ChcekerResult = NetWorkHelper.startCheck(NetWorkHelper.INTERNET_CONNECTION);
            return null;
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pdia = new ProgressDialog(LoginActivity.this);
            pdia.setMessage("Loading..");
            pdia.setTitle("Checking Network");
            pdia.setIndeterminate(false);
            pdia.setCancelable(true);
            pdia.show();
        }

        @Override
        protected void onPostExecute(Object result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            pdia.dismiss();
            if (!ChcekerResult) {
                AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this)
                        .setTitle("ไม่สามารถเชื่อมต่อ Internet")
                        .setMessage("กรุณาเชื่อมติดต่อ Internet และทำการเปิด Application ใหม่อีกครั้ง")
                        .setPositiveButton("ตกลง", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                System.exit(0);
                            }
                        });

                builder.show();

            }

        }

    }

    private class logInTask extends AsyncTask<String, Void, Object> {

        @Override
        protected Object doInBackground(String... params) {
            // TODO Auto-generated method stub
            return null;
        }

    }
}
